package com.jtny.nytb.equipment.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jtny.component.core.util.PageUtils;
import com.jtny.component.core.util.QueryWrapperUtils;
import com.jtny.component.dto.CurrentUserDTO;
import com.jtny.component.entity.LocalHolder;
import com.jtny.component.entity.PageData;
import com.jtny.component.enums.GenericStatusEnum;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.Assert;
import com.jtny.component.util.BeanUtils;
import com.jtny.component.util.CurrentUserUtils;
import com.jtny.nytb.config.datafactor.dto.ConfigLabelDTO;
import com.jtny.nytb.config.datafactor.service.DataParamEquipmentExtendService;
import com.jtny.nytb.equipment.dao.equipment.*;
import com.jtny.nytb.equipment.domain.equipment.EquipmentInfoDO;
import com.jtny.nytb.equipment.dto.equipment.EquipmentModelParamDTO;
import com.jtny.nytb.equipment.dto.equipment.EquipmentModelQueryPageParamDTO;
import com.jtny.nytb.equipment.dto.dataparam.LabelTempDTO;
import com.jtny.nytb.equipment.dto.device.*;
import com.jtny.nytb.equipment.enums.CodeRuleTypeEnum;
import com.jtny.nytb.equipment.enums.DataParamImportEnum;
import com.jtny.nytb.equipment.errorcode.EquipmentInfoErrorCodeEnum;
import com.jtny.nytb.equipment.service.*;
import com.jtny.nytb.equipment.vo.dataparam.LabelVO;
import com.jtny.nytb.equipment.vo.device.*;
import com.jtny.nytb.equipment.vo.model.EquipmentModelParamTempVO;
import com.jtny.nytb.equipment.vo.model.EquipmentModelParamVO;
import com.jtny.nytb.equipment.vo.model.EquipmentModelQueryPageParamTempVO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 智慧能源中心 - zx
 * @package com.jtny.nytb.equipment.biz.impl
 * @fileName EquipmentInfoBizImpl.java
 * @createTime 2023年09月20日 09:47
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */

@Service
@AllArgsConstructor
public class EquipmentInfoExtraServiceImpl implements EquipmentInfoExtraService {
    private EquipmentInfoDAO equipmentInfoDAO;
    private EquipmentParamDAO equipmentParamDAO;
    private EquipmentLabelDAO equipmentLabelDAO;
    private EquipmentSNDAO equipmentSNDAO;
    private EquipmentGatewayRelationDAO equipmentGatewayRelationDAO;

    private EquipmentInfoService equipmentInfoClient;
    private DataParamEquipmentExtendService dataParamEquipmentExtendClient;
    private CodeRuleService codeRuleClient;
    private EquipmentModelService equipmentModelClient;
    private DataParamService dataParamService;

    /**
     * 设备分页查询操作
     *
     * @param equipmentInfoQueryPageParamVO
     * @return
     * @throws ServiceException
     */
    @Override
    public PageData<EquipmentInfoVO> queryPage(EquipmentInfoQueryPageParamVO equipmentInfoQueryPageParamVO) throws ServiceException {
        //分页查询设备信息
        EquipmentInfoQueryPageParamDTO equipmentInfoQueryPageParamDTO = BeanUtils.copyToObject(equipmentInfoQueryPageParamVO, EquipmentInfoQueryPageParamDTO.class);
        Page<EquipmentInfoDO> page = Page.of(equipmentInfoQueryPageParamVO.getPageNo(), equipmentInfoQueryPageParamVO.getPageSize());
        PageData<EquipmentInfoDO> pageDataForDO = PageUtils.getPageData(equipmentInfoDAO.selectPage(page, queryWrapperAll(equipmentInfoQueryPageParamDTO)), equipmentInfoQueryPageParamDTO);
        PageData<EquipmentInfoDTO> pageDataForDTO = pageDataForDO.transformData(EquipmentInfoDTO.class);
        if (pageDataForDTO == null || CollUtil.isEmpty(pageDataForDTO.getData())) {
            return new PageData<>();
        }

        Collection<EquipmentInfoDTO> equipmentInfoDTOCollection = pageDataForDTO.getData();
        List<EquipmentInfoVO> equipmentInfoVOList = new ArrayList<>(equipmentInfoDTOCollection.size());
        equipmentInfoDTOCollection.forEach(equipmentInfoDTO -> {
            EquipmentInfoVO equipmentInfoVO = this.getEquipmentModelVOByDTO(equipmentInfoDTO);
            equipmentInfoVOList.add(equipmentInfoVO);
        });

        PageData<EquipmentInfoVO> pageDataForVO = pageDataForDTO.transformExcludeData(EquipmentInfoVO.class);
        pageDataForVO.setData(equipmentInfoVOList);

        return pageDataForVO;
    }
    private QueryWrapper<EquipmentInfoDO> queryWrapperAll(EquipmentInfoQueryPageParamDTO pageParam) {
        return queryWrapper(pageParam, true);
    }

    private QueryWrapper<EquipmentInfoDO> queryWrapper(EquipmentInfoQueryPageParamDTO pageParam, Boolean isQueryAll) {
        QueryWrapper<EquipmentInfoDO> queryWrapper = QueryWrapperUtils.getQueryWrapper(pageParam);
        queryWrapper.orderByDesc("id");
        if (isQueryAll && pageParam.getStatus() == null) {
            queryWrapper.ne("status", GenericStatusEnum.DELETED.getValue());
        } else if (pageParam.getStatus() == null) {
            queryWrapper.eq("status", GenericStatusEnum.NORMAL.getValue());
        }
        return queryWrapper;
    }
    /**
     * 删除设备 和 设备关联的参量
     *
     * @param id              编号
     * @param deleteDataParam 删除数据参数
     */
    @Override
    public void deleteEquipmentAll(Long id, Boolean deleteDataParam) {
        //通过设备模型id获取设备模型DTO对象
        EquipmentInfoDTO equipmentInfoDTO = equipmentInfoClient.getById(id);
        if (equipmentInfoDTO == null) {
            throw new ServiceException(EquipmentInfoErrorCodeEnum.OBJECT_NOT_FIND.getErrorCode(), EquipmentInfoErrorCodeEnum.OBJECT_NOT_FIND.getErrorMessage());
        }

        //获取当前登录用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装待修改的设备DTO对象
        equipmentInfoDTO.setStatus(GenericStatusEnum.DELETED.getValue());
        CurrentUserUtils.setUpdateDefaultField(equipmentInfoDTO, currentUserDTO);

        //修改操作
        equipmentInfoClient.update(equipmentInfoDTO);
        ArrayList<Long> equipmentIds = CollUtil.newArrayList(id);
        if (deleteDataParam) {
            dataParamEquipmentExtendClient.deleteByDataParamEquipmentIds(equipmentIds);
        } else {
            dataParamEquipmentExtendClient.deleteByEquipmentIds(equipmentIds);
        }
    }

    /**
     * 通过设备id查询单个设备信息
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentInfoVO detailsEqu(Long id) throws ServiceException {
        EquipmentInfoDTO equipmentInfoDTO = equipmentInfoClient.getById(id);
        return BeanUtils.copyToObject(equipmentInfoDTO, EquipmentInfoVO.class);
    }

    /**
     * 更新设备状态
     *
     * @param id     编号
     * @param status 地位
     */
    @Override
    public void updateEquipmentStatus(Long id, Integer status) {
        equipmentInfoClient.updateEquipmentStatus(id, status);
        dataParamEquipmentExtendClient.updateEquipmentDataParamStatus(id, status);
    }


    /**
     * 设备参数查询分页操作
     *
     * @param equipmentInfoParamPageQueryVO
     * @return
     * @throws ServiceException
     */
    @Override
    public PageData<EquipmentInfoParamVO> queryPageParam(EquipmentInfoParamPageQueryVO equipmentInfoParamPageQueryVO) throws ServiceException {
        //分页查询设备模型信息
        EquipmentInfoParamPageQueryDTO equipmentInfoParamPageQueryDTO = BeanUtils.copyToObject(equipmentInfoParamPageQueryVO, EquipmentInfoParamPageQueryDTO.class);
        PageData<EquipmentInfoParamDTO> pageDataForDTO = equipmentInfoClient.queryPageParam(equipmentInfoParamPageQueryDTO);
        return pageDataForDTO.transformData(EquipmentInfoParamVO.class);
    }

    /**
     * 设备参数信息添加操作
     *
     * @param equipmentInfoParamVO
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentInfoParamVO savaParam(EquipmentInfoParamVO equipmentInfoParamVO) throws ServiceException {
        //获取当前用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();

        //封装DTO对象
        EquipmentInfoParamDTO equipmentInfoParamDTOTwo = BeanUtils.copyToObject(equipmentInfoParamVO, EquipmentInfoParamDTO.class);
        equipmentInfoParamDTOTwo.setStatus(GenericStatusEnum.NORMAL.getValue());
        CurrentUserUtils.setCreateDefaultField(equipmentInfoParamDTOTwo, currentUserDTO);
        //保存设备参数信息
        EquipmentInfoParamDTO equipmentInfoParamDTOTwoFirst = equipmentInfoClient.savaParam(equipmentInfoParamDTOTwo);


        return BeanUtils.copyToObject(equipmentInfoParamDTOTwoFirst, EquipmentInfoParamVO.class);
    }

    /**
     * 设备参数信息删除操作
     *
     * @param id
     * @throws ServiceException
     */
    @Override
    public void deleteEquParam(Long id) throws ServiceException {
        //通过设备模型参数id获取设备模型参数DTO对象
        EquipmentInfoParamDTO equipmentInfoParamDTOTwo = equipmentInfoClient.getByIdParam(id);
        if (equipmentInfoParamDTOTwo == null) {
            throw new ServiceException(EquipmentInfoErrorCodeEnum.OBJECT_PARAM_NOT_FIND.getErrorCode(), EquipmentInfoErrorCodeEnum.OBJECT_PARAM_NOT_FIND.getErrorMessage());
        }

        //获取当前登录用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装待修改的用户DTO对象
        equipmentInfoParamDTOTwo.setStatus(GenericStatusEnum.DELETED.getValue());
        CurrentUserUtils.setUpdateDefaultField(equipmentInfoParamDTOTwo, currentUserDTO);

        //修改操作
        equipmentInfoClient.updateParam(equipmentInfoParamDTOTwo);
    }

    /**
     * 通过code查询单个设备信息
     *
     * @param code
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentInfoVO detailsEquCode(String code) throws ServiceException {
        EquipmentInfoDTO equipmentInfoDTO = equipmentInfoClient.detailsEquCode(code);
        if (equipmentInfoDTO == null) {
            throw new ServiceException(EquipmentInfoErrorCodeEnum.OBJECT_NOT_FIND.getErrorCode(), EquipmentInfoErrorCodeEnum.OBJECT_NOT_FIND.getErrorMessage());
        }
        return BeanUtils.copyToObject(equipmentInfoDTO, EquipmentInfoVO.class);

    }

    /**
     * 创建设备
     *
     * @param equipmentCodeGenerate 设备代码生成
     * @return {@link EquipmentInfoVO}
     */
    @Override
    public EquipmentInfoVO createEquipmentTemp(EquipmentCodeGenerateVO equipmentCodeGenerate) {
        String code = createEquipmentCodeByRule(equipmentCodeGenerate.getIsMeasuringPointDevice(), equipmentCodeGenerate.getEquipmentCodeRule());
        EquipmentInfoDTO equipmentInfoDTO = BeanUtils.copyToObject(equipmentCodeGenerate, EquipmentInfoDTO.class);
        equipmentInfoDTO.setStatus(GenericStatusEnum.DELETED.getValue());
        equipmentInfoDTO.setCode(code);

        EquipmentInfoDTO save = equipmentInfoClient.save(equipmentInfoDTO);
        // 如果是模版生成的还要保存模版带来的参数
        Long equipmentModelId = equipmentCodeGenerate.getEquipmentModelId();
        if (ObjectUtil.isNotNull(equipmentModelId)) {
            EquipmentModelQueryPageParamDTO query = new EquipmentModelQueryPageParamDTO();
            query.setPageSize(10000);
            query.setModelId(equipmentModelId);
            PageData<EquipmentModelParamTempVO> data = equipmentModelClient.queryPages(BeanUtils.copyToObject(query, EquipmentModelQueryPageParamTempVO.class));
            saveParamList(save.getId(), BeanUtils.copyToList(data.getData(), EquipmentInfoParamVO.class));
        }

        return BeanUtils.copyToObject(save, EquipmentInfoVO.class);
    }

    /**
     * 通过规则 生成 设备code
     *
     * @param equipmentCodeRule 规则
     * @return {@link String}
     */
    @Override
    public String createEquipmentCodeByRule(Boolean isMeasuringPointDevice, String equipmentCodeRule) {
        List<String> list = Arrays.asList(equipmentCodeRule.split(StrUtil.COMMA));
        String type = isMeasuringPointDevice ? CodeRuleTypeEnum.METER.getType() : CodeRuleTypeEnum.GENERAL.getType();
        return codeRuleClient.getCodeByCodeRuleId(type, list);
    }

    /**
     * 保存设备 正常操作
     *
     * @param equipmentNormal 设备基本信息
     */
    @Override
    public void saveEquipment4Normal(EquipmentNormalFormVO equipmentNormal) {
        // 保存基础信息
        EquipmentInfoDTO equipmentInfo = updateBaseInfo(equipmentNormal);
        Long equipmentId = equipmentInfo.getId();
        // 设备其他参数信息
        saveParamList(equipmentId, equipmentNormal.getParamList());
        // 保存关联标签
//        List<LabelVO> labelList = listEquipmentLabelByEquipment(equipmentId);
//        saveEquipmentLabelList(equipmentId, labelList);

        // 保存关联参量信息
        String equipmentCodeRule = equipmentNormal.getEquipmentCodeRule();
        List<String> ruleIdList = Arrays.asList(equipmentCodeRule.split(StrUtil.COMMA));
        dataParamService.moveDataParamByRuleDictIds(CodeRuleTypeEnum.getEnum(equipmentNormal.getIsMeasuringPointDevice()),
                ruleIdList, equipmentId, equipmentInfo.getCode(), equipmentInfo.getName(),null);
    }

    /**
     * 保存设备 模型操作
     *
     * @param equipmentModelFormVO 设备型号 VO
     */
    @Override
    public void saveEquipment4Model(EquipmentModelFormVO equipmentModelFormVO) {
        // 保存基础信息
        EquipmentInfoDTO equipmentInfo = updateBaseInfo(equipmentModelFormVO);
        Long equipmentId = equipmentInfo.getId();
        // 设备其他参数信息
        saveParamList(equipmentId, equipmentModelFormVO.getParamList());
        // 保存关联标签
//        List<LabelVO> labelList = listEquipmentLabelByEquipment(equipmentId);
//        saveEquipmentLabelList(equipmentId, labelList);

        // 保存关联参量信息
        Long deviceModelId = equipmentModelFormVO.getDeviceModelId();
        dataParamService.moveDataParamByModelId(CodeRuleTypeEnum.getEnum(equipmentModelFormVO.getIsMeasuringPointDevice()),
                deviceModelId, equipmentId, equipmentInfo.getCode(), equipmentInfo.getName(), null);
    }

    /**
     * 复制设备
     *
     * @param copyId 复制设备ID
     * @return {@link EquipmentInfoVO}
     */
    @Override
    public EquipmentInfoVO copyEquipment(Long copyId) {
        // 要复制的设备
        EquipmentInfoVO equipmentInfoVO = detailsEqu(copyId);
        // 复制的设备标签
        List<EquipmentInfoParamVO> paramList = listEquipmentParamByEquipmentId(copyId);
        // 复制的设备标签
        List<LabelVO> labelList = listEquipmentLabelByEquipment(copyId);
        // 保存基础信息
        EquipmentInfoDTO equipmentInfoNew = saveBaseInfo(BeanUtils.copyToObject(equipmentInfoVO, EquipmentBaseFromVO.class));
        Long equipmentId = equipmentInfoNew.getId();
        // 设备其他参数信息
        saveParamList(equipmentId, paramList);
        // 保存关联标签
//        saveEquipmentLabelList(equipmentId, labelList);
        // 保存关联参量信息
        dataParamService.copyDataParam(copyId, equipmentInfoVO.getCode(), equipmentId, equipmentInfoNew.getCode(), equipmentInfoNew.getName(), labelList);
        return BeanUtils.copyToObject(equipmentInfoNew, EquipmentInfoVO.class);
    }

    private List<EquipmentInfoParamVO> listEquipmentParamByEquipmentId(Long copyId) {
        EquipmentInfoParamPageQueryDTO query = new EquipmentInfoParamPageQueryDTO();
        query.setEquipmentId(copyId);
        query.setPageSize(1000);
        PageData<EquipmentInfoParamDTO> data = equipmentInfoClient.queryPageParam(query);

        Collection<EquipmentInfoParamDTO> tempList = data.getData();
        if (CollUtil.isEmpty(tempList)) {
            return CollUtil.newArrayList();
        }
        return tempList.stream().map(item -> {
            item.setId(null);
            item.setEquipmentId(null);
            return BeanUtils.copyToObject(item, EquipmentInfoParamVO.class);
        }).collect(Collectors.toList());
    }

    /**
     * 保存参数列表
     *
     * @param equipmentId 设备 ID
     * @param paramList   参数列表
     */
    private void saveParamList(Long equipmentId, List<EquipmentInfoParamVO> paramList) {
        if (CollUtil.isEmpty(paramList)) {
            return;
        }

        List<EquipmentInfoParamDTO> equipmentInfoParamList = new ArrayList<>();
        paramList.forEach(equipmentInfoParamVO -> {
            EquipmentInfoParamDTO equipmentInfoParamDTOTwo = BeanUtils.copyToObject(equipmentInfoParamVO, EquipmentInfoParamDTO.class);
            equipmentInfoParamDTOTwo.setEquipmentId(equipmentId);
            equipmentInfoParamDTOTwo.setId(null);
            equipmentInfoParamDTOTwo.setStatus(GenericStatusEnum.NORMAL.getValue());
            equipmentInfoParamList.add(equipmentInfoParamDTOTwo);
        });
        equipmentInfoClient.saveParamList(equipmentInfoParamList);
    }

    /**
     * 保存基础信息
     *
     * @param equipmentBase 设备基地
     * @return {@link EquipmentInfoDTO}
     */
    private EquipmentInfoDTO saveBaseInfo(EquipmentBaseFromVO equipmentBase) {
        String newCode = createEquipmentCodeByRule(equipmentBase.getIsMeasuringPointDevice(), equipmentBase.getEquipmentCodeRule());
        equipmentBase.setCode(newCode);
        equipmentBase.setName(equipmentBase.getName() + "副本" + DateUtil.thisMinute() + StrUtil.DASHED + DateUtil.thisSecond());
        //获取当前用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装DTO对象
        EquipmentInfoDTO equipmentInfoDTO = BeanUtils.copyToObject(equipmentBase, EquipmentInfoDTO.class);
        equipmentInfoDTO.setStatus(GenericStatusEnum.DISABLE.getValue());
        CurrentUserUtils.setCreateDefaultField(equipmentInfoDTO, currentUserDTO);
        // todo 名字后期考虑加顺序号
        //保存设备参数信息
        equipmentInfoDTO.setId(null);
        equipmentInfoDTO.setDockIdentifier(null);
        return equipmentInfoClient.save(equipmentInfoDTO);
    }

    /**
     * 保存基础信息
     *
     * @param equipmentBase 设备基地
     * @return {@link EquipmentInfoDTO}
     */
    private EquipmentInfoDTO updateBaseInfo(EquipmentBaseFromVO equipmentBase) {
        //获取当前用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装DTO对象
        EquipmentInfoDTO equipmentInfoDTO = BeanUtils.copyToObject(equipmentBase, EquipmentInfoDTO.class);
        equipmentInfoDTO.setStatus(GenericStatusEnum.DISABLE.getValue());
        CurrentUserUtils.setUpdateDefaultField(equipmentInfoDTO, currentUserDTO);
        // todo 名字后期考虑加顺序号
        //保存设备参数信息
        return equipmentInfoClient.update(equipmentInfoDTO);
    }


    /**
     * 设备信息修改操作
     *
     * @param equipmentInfoVO
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentInfoVO update(EquipmentInfoVO equipmentInfoVO) throws ServiceException {
        Long equipmentInfoVOId = equipmentInfoVO.getId();
        if (equipmentInfoVOId == null) {
            throw new ServiceException(EquipmentInfoErrorCodeEnum.PARAM_DEVICE_ID_IS_NULL.getErrorCode(), EquipmentInfoErrorCodeEnum.PARAM_DEVICE_ID_IS_NULL.getErrorMessage());
        }
        //通过设备id获取设备DTO对象
        EquipmentInfoDTO equipmentInfoDTO = equipmentInfoClient.getById(equipmentInfoVOId);
        if (equipmentInfoDTO == null) {
            throw new ServiceException(EquipmentInfoErrorCodeEnum.OBJECT_NOT_FIND.getErrorCode(), EquipmentInfoErrorCodeEnum.OBJECT_NOT_FIND.getErrorMessage());
        }
        //获取当前登录用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装待修改的设备模型DTO对象
        BeanUtils.copyObject(equipmentInfoVO, equipmentInfoDTO);
        CurrentUserUtils.setUpdateDefaultField(equipmentInfoDTO, currentUserDTO);
        EquipmentInfoDTO equipmentInfoDTOFirst = equipmentInfoClient.getById(equipmentInfoVO.getId());
        //
        List<DataParamCoreDTO> dataParamCoreList = new ArrayList<>();
        if (!(StringUtils.isEmpty(equipmentInfoVO.getEquipmentCodeRule()) ||
                equipmentInfoDTOFirst.getEquipmentCodeRule().equals(equipmentInfoVO.getEquipmentCodeRule()))) {
            String equipmentCodeRule = equipmentInfoVO.getEquipmentCodeRule();
            // 生成设备编号
            equipmentInfoDTO.setCode(getEquipmentCode(equipmentCodeRule,
                    equipmentInfoVO.getIsMeasuringPointDevice() ?
                            DataParamImportEnum.METER_EQUIPMENT : DataParamImportEnum.EQUIPMENT));
        }
        if (equipmentInfoVO.getRuleIdList() != null) {
            dataParamService.backDataParam(equipmentInfoVO.getId(), equipmentInfoVO.getCode());
            dataParamService.moveDataParamByRuleDictIds(CodeRuleTypeEnum.getEnum(equipmentInfoVO.getIsMeasuringPointDevice()),
                    equipmentInfoVO.getRuleIdList(), equipmentInfoVO.getId(), equipmentInfoVO.getCode(), equipmentInfoVO.getName(), null);
        }
        //修改操作
        equipmentInfoClient.update(equipmentInfoDTO);
        return BeanUtils.copyToObject(equipmentInfoDTO, EquipmentInfoVO.class);
    }

    /**
     * 更新设备
     *
     * @param equipmentInfoVO 设备信息 VO
     * @throws ServiceException 服务异常
     */
    @Override
    public void updateEquipment(EquipmentInfoVO equipmentInfoVO) throws ServiceException {
        Long equipmentInfoId = equipmentInfoVO.getId();
        EquipmentInfoDTO old = equipmentInfoClient.getById(equipmentInfoId);
        Assert.notNull(old, "设备不存在");

        //封装DTO对象
        EquipmentInfoDTO equipmentInfoDTO = BeanUtils.copyToObject(equipmentInfoVO, EquipmentInfoDTO.class);
        //获取当前用户
        CurrentUserUtils.setUpdateDefaultField(equipmentInfoDTO, LocalHolder.getCurrentUser());
        equipmentInfoClient.update(equipmentInfoDTO);
        // 是否需要联动修改参量名称
        if (!old.getName().equals(equipmentInfoVO.getName())) {
            dataParamEquipmentExtendClient.updateDataParamName(equipmentInfoId, equipmentInfoVO.getName());
        }
    }

    /**
     * 按设备id 查询设备标签
     *
     * @param equipmentId 设备 ID
     * @return {@link List}<{@link LabelVO}>
     */
    @Override
    public List<LabelVO> listEquipmentLabelByEquipment(Long equipmentId) {
        List<LabelTempDTO> labelTempDTOList = equipmentInfoClient.listEquipmentLabelByEquipmentId(equipmentId);
        return BeanUtils.copyToList(labelTempDTOList, LabelVO.class);
    }

    /**
     * 保存设备标签
     *
     * @param equipmentId 设备 ID
     * @param labelList   标签列表
     */
//    @Override
//    public void saveEquipmentLabelList(Long equipmentId, List<LabelVO> labelList) {
//        if (CollUtil.isNotEmpty(labelList)) {
//            // 新增设备标签
//            equipmentInfoClient.saveEquipmentLabelList(equipmentId, BeanUtils.copyToList(labelList, LabelTempDTO.class));
//            // 同步参量
//            dataParamEquipmentExtendClient.addDataParamLabelList(equipmentId, BeanUtils.copyToList(labelList, ConfigLabelDTO.class));
//        }
//    }

    /**
     * 删除设备标签
     *
     * @param equipmentId 设备 ID
     * @param labelItemId
     * @return {@link //EquipmentModelVO}
     */
//    @Override
//    public void deleteLabel(Long equipmentId, Long labelItemId) {
//        // 删除设备标签
//        equipmentInfoClient.deleteLabel(equipmentId, labelItemId);
//        // 同步参量
//        dataParamEquipmentExtendClient.deleteDataParamLabelList(equipmentId, CollUtil.newArrayList(labelItemId));
//    }

    /**
     * 按名称获取设备信息
     *
     * @param nameList 名称列表
     * @return {@link Map}<{@link String}, {@link EquipmentInfoDTO}>
     */
    @Override
    public Map<String, EquipmentInfoDTO> mapEquipmentByNameList(DataParamImportEnum type, List<String> nameList) {
        if (CollUtil.isNotEmpty(nameList)) {
            return equipmentInfoClient.mapEquipmentByNameList(type.getCodeRuleTypeEnum().getType(),
                    new HashSet<>(nameList));
        }
        return new HashMap<>();
    }

    /**
     * 通过设备上报标识List获取设备
     *
     * @param dockIdentifierList 设备上报标识List
     * @return {@link Map}<{@link String}, {@link EquipmentInfoDTO}>
     */
    @Override
    public Map<String, EquipmentInfoDTO> mapEquipmentByDockIdentifierList(DataParamImportEnum type, List<String> dockIdentifierList) {
        if (CollUtil.isNotEmpty(dockIdentifierList)) {
            return equipmentInfoClient.mapEquipmentByDockIdentifierList(type.getCodeRuleTypeEnum().getType(),
                    new HashSet<>(dockIdentifierList));
        }
        return new HashMap<>();
    }


    /**
     * 获取设备Code
     *
     * @param equipmentCodeRule 设备代码规则
     * @param typeEnum          类型
     * @return {@link String}
     */
    private String getEquipmentCode(String equipmentCodeRule, DataParamImportEnum typeEnum) {
        List<String> list = Arrays.asList(equipmentCodeRule.split(StrUtil.COMMA));
        return codeRuleClient.getCodeByCodeRuleId(typeEnum.getCodeRuleTypeEnum().getType(), list);
    }


    private EquipmentInfoVO getEquipmentModelVOByDTO(EquipmentInfoDTO equipmentInfoDTO) {
        EquipmentInfoVO equipmentInfoVO = BeanUtils.copyToObject(equipmentInfoDTO, EquipmentInfoVO.class);
        return equipmentInfoVO;
    }
}
